package com.xinqi.modules.stats.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.stats.score.convert.CourseTotalScoreOfflineConfigConvert;
import com.xinqi.modules.stats.score.domain.CourseTotalScoreBaseConfigEntity;
import com.xinqi.modules.stats.score.domain.CourseTotalScoreOfflineConfigEntity;
import com.xinqi.modules.stats.score.dto.req.CourseTotalScoreOfflineConfigQueryDTO;
import com.xinqi.modules.stats.score.dto.req.CourseTotalScoreOfflineConfigSetDTO;
import com.xinqi.modules.stats.score.dto.rsp.CourseTotalScoreOfflineConfigResultDTO;
import com.xinqi.modules.stats.score.mapper.CourseTotalScoreBaseConfigMapper;
import com.xinqi.modules.stats.score.mapper.CourseTotalScoreOfflineConfigMapper;
import com.xinqi.modules.stats.score.service.CourseTotalScoreOfflineConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;


/**
 * 线下成绩等级分值设置表服务层实现
 *
 * @author: system
 * @date: 2023/03/26
 */
@Service("courseTotalScoreOfflineConfigService")
@RequiredArgsConstructor
public class CourseTotalScoreOfflineConfigServiceImpl extends BaseServiceImpl<CourseTotalScoreOfflineConfigMapper, CourseTotalScoreOfflineConfigEntity> implements CourseTotalScoreOfflineConfigService {

    private final CourseTotalScoreOfflineConfigConvert courseTotalScoreOfflineConfigConvert;

    private final CourseTotalScoreBaseConfigMapper courseTotalScoreBaseConfigMapper;

    /**
     * 初始化课程栏目等级分制配置
     */
    @Override
    public List<CourseTotalScoreOfflineConfigResultDTO> initCourseTotalScoreOfflineConfig(CourseTotalScoreOfflineConfigQueryDTO dto) {
        // 先删除
        this.baseMapper.deleteOfflineConfig(dto.getOfflineScoreId());
        // 后初始化
        LambdaQueryWrapper<CourseTotalScoreBaseConfigEntity> wrapper = Queries.lambda();
        wrapper.orderByAsc(CourseTotalScoreBaseConfigEntity::getSort);
        List<CourseTotalScoreBaseConfigEntity> baseConfigEntities = courseTotalScoreBaseConfigMapper.selectList(wrapper);
        // 所有有的实体类转换 mapstract方式
        AtomicInteger index = new AtomicInteger(1);
        List<CourseTotalScoreOfflineConfigEntity> typeConfigEntities = baseConfigEntities.stream().map(v -> {
            CourseTotalScoreOfflineConfigEntity configEntity = new CourseTotalScoreOfflineConfigEntity();
            configEntity.setOfflineScoreId(dto.getOfflineScoreId());
            configEntity.setMax(v.getMax());
            configEntity.setMin(v.getMin());
            configEntity.setGrade(v.getGrade());
            configEntity.setSort(index.getAndIncrement());
            return configEntity;
        }).collect(Collectors.toList());
        if (this.saveBatch(typeConfigEntities)) {
            return this.findCourseTotalScoreOfflineConfig(dto);
        } else {
            return null;
        }
    }

    /**
     * 查询课程栏目等级分制
     */
    @Override
    public List<CourseTotalScoreOfflineConfigResultDTO> findCourseTotalScoreOfflineConfig(CourseTotalScoreOfflineConfigQueryDTO dto) {
        LambdaQueryWrapper<CourseTotalScoreOfflineConfigEntity> wrapper = Queries.lambda();
        wrapper.eq(CourseTotalScoreOfflineConfigEntity::getOfflineScoreId, dto.getOfflineScoreId());
        List<CourseTotalScoreOfflineConfigResultDTO> list = this.baseMapper.selectDtoList(wrapper, courseTotalScoreOfflineConfigConvert::convert);
        if (list.size() > 0) {
            return list;
        } else {
            this.initCourseTotalScoreOfflineConfig(dto);
            list = this.baseMapper.selectDtoList(wrapper, courseTotalScoreOfflineConfigConvert::convert);
        }
        return list;
    }

    /**
     * 设置课程栏目等级分制
     */
    @Override
    public boolean setCourseTotalScoreOfflineConfig(CourseTotalScoreOfflineConfigSetDTO dto) {
        CommonErrors.BAD_REQUEST.check(dto.getList() != null && dto.getList().size() < 7, MessageUtils.message("course.content.total.score.config.valid"));
        dto.getList().forEach(item -> {
            CommonErrors.BAD_REQUEST.check(item.getMax() <= 100, MessageUtils.message("等级分值上限不能超过100"));
        });
        // 先删除
        this.baseMapper.deleteOfflineConfig(dto.getOfflineScoreId());
        // 后添加
        AtomicInteger index = new AtomicInteger(1);
        List<CourseTotalScoreOfflineConfigEntity> list = dto.getList().stream().map(v -> {
            int i = index.getAndIncrement();
            if (i > 1) {
                CommonErrors.BAD_REQUEST.check(dto.getList().get(i - 2).getMin().equals(dto.getList().get(i - 1).getMax()), "等级分制区间设置不合理");
            }
            CourseTotalScoreOfflineConfigEntity entity = new CourseTotalScoreOfflineConfigEntity();
            entity.setOfflineScoreId(dto.getOfflineScoreId());
            entity.setMax(v.getMax());
            entity.setMin(v.getMin());
            entity.setGrade(v.getGrade());
            entity.setSort(i);
            return entity;
        }).collect(Collectors.toList());
        return this.saveBatch(list);
    }

}
